<template>
  <div class="w-pdf">
    <div class="top-bar">
      <div class="left">
        <img
          width="30"
          class="w-pointer w-hover-glow"
          src="../../../assets/global/left.png"
          @click="leftBtnClick"
          alt
        >
        <img
          width="30"
          class="w-pointer w-hover-glow"
          src="../../../assets/global/right.png"
          @click="rightBtnClick"
          alt
        >
      </div>
      <div class="center">
        <span class="page-total">共 {{ total }} 页</span>
        <input type="number" v-model.number="inputPage" class="input-page">
      </div>
      <div class="right">
        <img
          v-if="isShowClose"
          width="30"
          class="w-pointer w-hover-glow"
          src="../../../assets/global/right.png"
          @click="closeBtnClick"
          alt
        >
      </div>
    </div>
    <vue-pdf
      class="vue-pdf"
      :src="url"
      :page="currentPage"
      @progress="progress"
      @loaded="loaded"
      @page-loaded="pageLoaded"
      @num-pages="numPages"
      @error="error"
      @link-clicked="linkClicked"
    />
    <img v-if="loading" class="loading-img" src="@/assets/ImageLazy/loading.png" alt>
  </div>
</template>

<script>
import vuePdf from "vue-pdf";
export default {
  name: "WPDF",
  components: {
    vuePdf
  },
  data() {
    return {
      // 当前页数
      currentPage: 1,
      // 总页数
      total: 0,
      // 是否加载中
      loading: true,
      // 输入页数
      inputPage: 1
    };
  },
  props: {
    // pdf 地址
    url: {
      type: String,
      default: ""
    },
    // 是否显示关闭按钮
    isShowClose: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    inputPage(val) {
      if (typeof val === "number" && val > 0 && val <= this.total) {
        this.currentPage = val;
      }
    }
  },
  computed: {
    leftBtnEnable() {
      return this.currentPage > 1;
    },
    rightBtnEnable() {
      return this.currentPage < this.total;
    }
  },
  methods: {
    progress() {},
    loaded() {
      this.loading = false;
    },
    pageLoaded() {},
    numPages(num) {
      this.total = num;
    },
    linkClicked(x) {
      console.log("linkClicked = ", x);
    },
    error() {
      this.loading = false;
      this.$emit("error");
    },
    leftBtnClick() {
      let page = this.currentPage;
      if (page > 1) {
        page -= 1;
      }
      this.currentPage = page;
      this.inputPage = page;
    },
    rightBtnClick() {
      let page = this.currentPage;
      if (page < this.total) {
        page += 1;
      }
      this.currentPage = page;
      this.inputPage = page;
    },
    // 关闭按钮点击
    closeBtnClick() {
      this.$emit("close");
    }
  }
};
</script>

<style lang="scss" scoped>
.w-pdf {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  min-height: 100%;
  background-color: #fff;
  overflow: scroll;
  padding-top: 44px;
  .top-bar {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    background-color: #7ab1f4;
    z-index: 100;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    .page-total {
      color: #666;
    }
    .input-page {
      width: 70px;
      outline: none;
    }
  }

  .vue-pdf {
    max-width: 1200px;
    margin: 0 auto;
  }

  .w-hover-glow:hover,
  .w-hover-glow:focus,
  .w-hover-glow:active {
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.6);
  }

  .w-pointer {
    cursor: pointer;
  }

  @-webkit-keyframes rotation {
    from {
      -webkit-transform: rotate(0deg);
    }
    to {
      -webkit-transform: rotate(360deg);
    }
  }

  .loading-img {
    width: 60px;
    height: 60px;
    vertical-align: middle;
    -webkit-transform: rotate(360deg);
    animation: rotation 3s linear infinite;
    -moz-animation: rotation 3s linear infinite;
    -webkit-animation: rotation 3s linear infinite;
    -o-animation: rotation 3s linear infinite;
    position: fixed;
    left: 50%;
    top: 50%;
    margin-left: -30px;
    margin-top: -30px;
  }
}
</style>
